<?php
class Macys_Api_V2_Catalog_Category_Response {

	private $_data = null;
	private $_filterParams = null;
	private $log;

	public function __construct($categoryResponse, $filterParams) {
		$this->log = Zend_Registry::get('log');
		$this->_data = $categoryResponse;
		$this->_filterParams = $filterParams;
	}


	public function getProducts() {
		if(isset($this->_data["products"]))
		return $this->_data["products"];
		return null;
	}
	
	public function getInputs() {
		if(isset($this->_data["inputs"])) {
			return $this->_data["inputs"];
		}
		return null;
	}

	public function getLeftOutSummaryInfo() {
		$retVal = array();
		foreach ($this->_data as $key=>$value) { 
			if(!is_array($value)) {
				$retVal[$key] = $value;
			}
		}
		return $retVal;
	}
	
	public function getCategoryRefinements() {
		if(isset($this->_data["availableRefinements"])) {
			return $this->_data["availableRefinements"];
		}
		else {
			return false;
		}
	}

	public function getRefinementsNavigation() {
		$parent = null;
		if($this->getCategoryRefinements()) {
			$ref = $this->getCategoryRefinements();
			foreach ($ref as $value) {
				$parent[] = $this->refinmentNavigationPages($value);					
			}
		}
		return $parent;

	}

	private function refinmentNavigationPages($value) {
		$parent = array();
		$parent['module'] = 'default';
		$parent['controller'] = 'category';
		$parent['action'] = 'getcategoryv2';
		$parent['label'] = $value["displayName"];
		$dispName = $value["displayName"];
		$actName = $value["actualName"];
		foreach ($value["refinementValues"] as $k=>$v) {
			$parent['pages'][] = $this->refinementNavigationPage($v["displayValue"] ." (" . $v["productCount"] .") ", $actName, $v["displayValue"]);
			$this->log->debug("refinementValues = " . $actName . " : ". $v["displayValue"]);
		}
		return $parent;
	}


	private function refinementNavigationPage($displayName, $key, $value) {
		$parent['module'] = 'default';
		$parent['controller'] = 'category';
		$parent['action'] = 'getcategoryv2';
		$parent['label'] = $displayName;
		$params = array_merge($this->_filterParams, array($key => $value));
		$parent['params'] = array_merge(array ('categoryId' => $this->getCurrentCategory()) ,
		$params);
		return $parent;
	}


	// Used by CategorySidePanel
	// in Macys_View_Helper_CategorySidePanel
	public function getCategoryNavigation() {

		if(isset($this->_data["categoryDetails"]))
		return  new Zend_Navigation_Page_Mvc($this->processCategories($this->_data["categoryDetails"]));
		//return  $this->processCategories($this->_data["categoryDetails"]);
		else
		return null;
	}

	private function processCategories($cat, $parent=null) {
		if(isset($cat["categoryName"])) {
			if(isset($cat["categoryId"])) {
				$parent['module'] = 'default';
				$parent['controller'] = 'category';
				$parent['action'] = 'getcategoryv2';
				$parent['label'] = $cat["categoryName"];
				$parent['params'] = array_merge(array ('categoryId' => $cat['categoryId']), $this->_filterParams);
				if(isset($cat["childCategories"])) {
					foreach($cat["childCategories"] as $a => $v) {
						$parent['pages'][] = new Zend_Navigation_Page_Mvc($this->processCategories($cat["childCategories"][$a]));
						//$parent['pages'][] = $this->processCategories($cat["childCategories"][$a]);
					}
				}
			}
		}
		return $parent;

	}

	public function getCurrentCategory() {
		if(isset($this->_data["categoryDetails"]["categoryId"]))
		return $this->_data["categoryDetails"]["categoryId"];
		return Macys_Api_V2_Catalog_Category::getInstance()->getCategoryId();
	}

	public function getRawResponse() {
		return $this->_data;
	}


}